/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.oms.flow.order;

import com.lxk.event.helper.EventHelper;
import com.lxk.event.oms.OrderCreateEvent;
import com.lxk.event.oms.OrderOptionLogEvent;
import com.lxk.oms.order.domain.vo.OmsOrderVo;
import com.lxk.service.ums.response.UmsUserDto;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;

import static com.lxk.oms.flow.order.FlowOrderNodeConst.ORDER_END_NODE;

/**
 * 下单结束节点
 *
 * @author heyi
 * 2024/8/21
 */
@LiteflowComponent(ORDER_END_NODE)
public class OrderEndNode extends NodeComponent {
    @Override
    public void process() throws Exception {
        OrderContext contextBean = getContextBean(OrderContext.class);
        OrderOptionLogEvent createEvent = new OrderOptionLogEvent();
        OmsOrderVo omsOrder = contextBean.getOrderResult().getOrder();
        UmsUserDto umsUserDto = contextBean.getUmsUserDto();
        Long merId = contextBean.getMerId();
        Long userId = umsUserDto.getUserId();
        String nickName = umsUserDto.getNickName();
        createEvent.setOrderSn(omsOrder.getOrderNo());
        createEvent.setUserId(userId);
        createEvent.setMerId(contextBean.getMerId());
        createEvent.setOrderLogEnums(OrderOptionLogEvent.OrderLogEnums.ORDER);
        createEvent.setUserType(umsUserDto.getUserType());
        createEvent.setContent(OrderOptionLogEvent.OrderContentFormatEnums.CREATE_ORDER.formatContent(
            nickName, omsOrder.getOrderNo()));
        EventHelper.publishEvent(createEvent);
    }

    @Override
    public boolean isAccess() {
        return getContextBean(OrderContext.class).getSaveDb();
    }

    @Override
    public void onSuccess() throws Exception {
        OrderCreateEvent orderCreateEvent = new OrderCreateEvent();
        OrderContext contextBean = getContextBean(OrderContext.class);
        OmsOrderVo omsOrder = contextBean.getOrderResult().getOrder();
        orderCreateEvent.setOrderNo(omsOrder.getOrderNo());
        orderCreateEvent.setUserId(omsOrder.getUserId());
        orderCreateEvent.setActPayPrice(omsOrder.getActPayPrice());
        orderCreateEvent.setTenantId(contextBean.getTenantId());
        EventHelper.publishEvent(orderCreateEvent);
    }
}
